package cn.ware.house.interfaces.controller.productcontroller;

import cn.ware.house.interfaces.entity.goods.Product;
import cn.ware.house.interfaces.entity.goods.ProductStyle;
import cn.ware.house.interfaces.service.productservice.ProductService;
import cn.ware.house.interfaces.vo.PagingVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductController {

    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    // 分页查询商品信息
    @GetMapping("/page")
    @ResponseStatus(HttpStatus.OK)
    public PagingVo<List<Product>> getProductsByPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        List<Product> products = productService.findProductsByPage(page, size);
        return new PagingVo<>(products,productService.getProductTotalCount());
    }

    // 根据商品编号查询商品信息
    @GetMapping("/{productId}")
    @ResponseStatus(HttpStatus.OK)
    public Product getProductById(@PathVariable Integer productId) {
        return productService.findProductById(productId);
    }

    // 添加商品信息
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public boolean insertProduct(@RequestBody Product product) {
        return productService.insertProduct(product);
    }

    // 根据商品编号更新商品信息
    @PutMapping("/{productId}")
    @ResponseStatus(HttpStatus.OK)
    public boolean updateProductById(@PathVariable Integer productId, @RequestBody Product product) {
        product.setProductId(productId);
        System.out.println(product.getStyles());
        return productService.updateProductById(product);
    }

    // 根据商品编号删除商品信息
    @DeleteMapping("/{productId}")
    @ResponseStatus(HttpStatus.OK)
    public boolean deleteProductById(@PathVariable Integer productId) {
        return productService.deleteProductById(productId);
    }

    // 根据商品编号获取该商品的所有样式信息
    @GetMapping("/{productId}/styles")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<List<ProductStyle>> getProductStyles(@PathVariable Integer productId) {
        List<ProductStyle> styles = productService.getProductStylesByProductId(productId);
        return new ResponseEntity<>(styles, HttpStatus.OK);
    }
}